package com.tian.authen.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * @author zhump
 */
@Log4j2
@Controller
@SessionAttributes({"authorizationRequest"})
public class OauthConfirmAccessEndpoint  {

    @RequestMapping(value = "/getLogin",method = RequestMethod.GET)
    public String loginPage() {
        return "login-page";
    }

    @RequestMapping(value = "/auth",method = RequestMethod.GET)
    public String indexPage(){
        return "auth";
    }

    @RequestMapping(value = "/mysqlTest")
    public String getAccessConfirmation(Map<String, Object> argMap, HttpServletRequest request,Model model) throws Exception {
        log.info("[getAccessConfirmation]");
        log.info("[getAccessConfirmation-argMap]:{}",JSON.toJSONString(argMap));
        AuthorizationRequest authorizationRequest = (AuthorizationRequest)argMap.get("authorizationRequest");
        if (null == authorizationRequest) {
            return "redirect:" + "login-page";
        }
        log.info("[AuthorizationRequest]:{}", JSON.toJSONString(authorizationRequest));
        model.addAttribute("scopes",authorizationRequest.getScope());
        HashMap<String, Object> clientMap = new HashMap<>();
        clientMap.put("clientId",authorizationRequest.getClientId());
        clientMap.put("name","admint");
        model.addAttribute("client",clientMap);
        return "oauth-grant";
    }
}
